package gov.nasa.anml.lifted;

import java.util.ArrayList;
import java.util.HashMap;

import gov.nasa.anml.PDDL;
import gov.nasa.anml.State;
import gov.nasa.anml.PDDL.ComplexAction;
import gov.nasa.anml.PDDL.FloatLiteral;
import gov.nasa.anml.utility.*;

public class Block extends Unit<ANMLBoolean,SimpleVoid> {

	// working on naming of blocks by line numbers
	public Block(Scope parent, SimpleString n) {
		super(parent,n);
	}

	public boolean apply(State p, int contextID, State c) {
		for (Statement s : statements)
			if (!s.apply(p,contextID,c))
				return false;
		return true;
	}

	public IdentifierCode idCode() {
		return IdentifierCode.Block;
	}

	public TypeCode typeCode() {
		return TypeCode.Boolean;
	}

	public void translateDecl(PDDL pddl, Interval unit) {
		// for a simple block, don't add a level of nesting to the translation.
		if(isSimple()) {
			_translateDecl(pddl,unit);
		} else {
			super.translateDecl(pddl,unit);
		}
	}
	
	public boolean isSimple() {
		if (this.parameters.count != 0)
			return false;
		if (this.fluents.count != 0)
			return false;
		if (this.fluentFunctions.count != 0)
			return false;
		if (this.decompositions.size() != 0)
			return false;
		return true;
	}

	public void translateStmt(PDDL pddl, Interval unit, PDDL.Time time) {
		/*
		int length = pddl.bufAppend(name);
		int sz = pddl.context.size();
		for (Parameter<?> p : parameters.table) {
			if (p.asPDDLParameter() != null) 
				pddl.context.add(p.asPDDLParameter);
		}
		++pddl.depth;
		*/
		
		if(isSimple()) {
			_translateStmt(pddl,unit,time); // instead of _translateStmt(pddl,this,PDDL.Time.Timeless);
		} else {
			super.translateStmt(pddl,unit,time);
		}
		
		/*
		pddl.bufReset(length);
		int l = pddl.context.size() - sz;
		while (--l >= 0)
			pddl.context.remove(sz);
		
		--pddl.depth;
		*/
	}

	// The reason to need this is if Block's don't have names that work.  
	// Currently, they may have names that work.  We shall see.
	/*
	public void translateDecl(PDDL pddl, CompoundTime unit) {
		if (asPDDLAction != null)
			return;
		
		int s = pddl.context.size();

		if (duration.init instanceof Parameter) {
			asPDDLAction = pddl.new ComplexAction();
		} else {
			asPDDLAction = pddl.new Action();
			if(duration.init != null) {
				asPDDLAction.duration = (PDDL.FloatExpression) duration.init.translateExpr(pddl,this);
			} else {
				if (duration.value != null)
					asPDDLAction.duration = pddl.new FloatLiteral(duration.value.v);
				else
					asPDDLAction.duration = pddl.One;
			}
		}
		
		for (PDDL.Parameter p : pddl.context) {
			asPDDLAction.context.add(p);
		}
		
		for (Parameter<?> p : parameters.table) {
			p.translateDecl(pddl,this);
			pddl.context.add(p.asPDDLParameter);
		}

		++pddl.depth;
		
		_translateDecl(pddl);
		
		int l = pddl.context.size() - s;
		while (--l >= 0)
			pddl.context.remove(s);
		
		--pddl.depth;
	}
	*/
	
}
